package org.example.operator;

import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.KeyedStream;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.example.bean.WaterSensor;

public class 聚合算子KeyByOperator {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);


        DataStreamSource<WaterSensor> source = env.fromElements(
                new WaterSensor("s1", 1L, 1),
                new WaterSensor("s2", 1L, 2),
                new WaterSensor("s1", 1L, 13));

        /**
         *
         */
        KeyedStream<WaterSensor, String> keyBy = source.keyBy(value -> {
            return value.getType();
        });

//        keyBy.sum("waterRecord").print();

        /*
            maxlmaxby的区别；
                max：只会取比较字段的最大值，非比较字段保留第一次的值
                maxBy：取比较字段的最大值，同时非比较字段取最大值这条数据的值
         */
//        keyBy.min("SensorRecordTimestamp").print();
        keyBy.minBy("SensorRecordTimestamp").print();


        env.execute();
    }
}

